package com.liliudong.rommath.api.sys.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.lang.tree.Tree;
import com.liliudong.rommath.biz.sys.entity.SysDept;
import com.liliudong.rommath.biz.sys.param.SysDeptAddParam;
import com.liliudong.rommath.biz.sys.param.SysDeptModifyParam;
import com.liliudong.rommath.biz.sys.service.SysDeptService;
import com.liliudong.rommath.common.R;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统部门 控制层。
 *
 * @author liudong.li
 * @since 1.0.0
 */
@RestController
@RequestMapping("/sys/dept")
@RequiredArgsConstructor
public class SysDeptController {

    private final SysDeptService sysDeptService;

    /**
     * 添加
     *
     * @param param param
     * @return {@link R }<{@link Void }>
     */
    @SaCheckPermission("sys:dept:add")
    @PostMapping
    public R<Void> add(@RequestBody @Validated SysDeptAddParam param) {
        sysDeptService.add(param);
        return R.ok();
    }

    /**
     * 删除
     *
     * @param id 主键
     * @return {@link R }<{@link Void }>
     */
    @SaCheckPermission("sys:dept:delete")
    @DeleteMapping("/{id}")
    public R<Void> delete(@PathVariable Long id) {
        sysDeptService.delete(id);
        return R.ok();
    }

    /**
     * 修改
     *
     * @param param param
     * @return {@link R }<{@link Void }>
     */
    @SaCheckPermission("sys:dept:modify")
    @PutMapping
    public R<Void> modify(@RequestBody @Validated SysDeptModifyParam param) {
        sysDeptService.modify(param);
        return R.ok();
    }

    /**
     * 详情
     *
     * @param id 主键
     * @return {@link R }<{@link SysDept }>
     */
    @SaCheckPermission("sys:dept:query")
    @GetMapping("/detail/{id}")
    public R<SysDept> detail(@PathVariable Long id) {
        SysDept sysDept = sysDeptService.getById(id);
        return R.ok(sysDept);
    }

    /**
     * 树结构
     *
     * @return {@link R }<{@link SysDept }>
     */
    @SaCheckPermission("sys:dept:query")
    @GetMapping("/tree")
    public R<List<Tree<String>>> tree() {
        List<Tree<String>> trees = sysDeptService.tree();
        return R.ok(trees);
    }

}